package com.foo.ajax;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.DefaultCssAutoCompleteTextField;
import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.resource.CssResourceReference;

public class GradePage extends WebPage {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String gradeType = "Letter";
	private String letterGrade;
	private int integerGrade;
	private WebMarkupContainer gradeDetail;
	private DropDownChoice<String> letterGradeChoice;
	private TextField<Integer> integerGradeBox;

	public GradePage() {
		Form<GradePage> f = new Form<GradePage>("f",
				new CompoundPropertyModel<GradePage>(this)) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				System.out.println(gradeType);
				System.out.println(isLetterGrade() ? letterGrade
						: integerGrade);
			}
		};
		add(f);
		AutoCompleteTextField<String> nameBox = new DefaultCssAutoCompleteTextField<String>(
				"name", null) {

			private static final long serialVersionUID = 1L;

			@Override
			protected Iterator<String> getChoices(String input) {
				return getNameMatches(input);
			}
		};
		f.add(nameBox);
		DropDownChoice<String> gradeTypeChoice = new DropDownChoice<String>(
				"gradeType",
				Arrays.asList(new String[] { "Letter", "Integer" }));
		f.add(gradeTypeChoice);
		gradeTypeChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.add(gradeDetail);
			}

		});
		gradeDetail = new WebMarkupContainer("gradeDetail");
		f.add(gradeDetail);
		gradeDetail.setOutputMarkupPlaceholderTag(true);
		letterGradeChoice = new DropDownChoice<String>("letterGrade",
				Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" })) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				return isLetterGrade();
			}
		};
		gradeDetail.add(letterGradeChoice);
		integerGradeBox = new TextField<Integer>("integerGrade") {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				return !isLetterGrade();
			}
		};
		gradeDetail.add(integerGradeBox);
	}

	private Iterator<String> getNameMatches(String input) {
		List<String> names = Arrays.asList(new String[] { "John", "Joan",
				"Judy", "Paul", "Peter" });
		List<String> matches = new ArrayList<String>();
		for (String name : names) {
			if (name.startsWith(input)) {
				matches.add(name);
			}
		}
		return matches.iterator();
	}


	@Override
	public void renderHead(IHeaderResponse response) {
		response.render(CssHeaderItem.forReference(new CssResourceReference(AutoCompleteTextField.class, "DefaultCssAutoCompleteTextField.css")));
	}

	private boolean isLetterGrade() {
		return gradeType.equals("Letter");
	}
}
